﻿Imports System
Imports System.Text
Imports System.IO
Imports System.ComponentModel


''' <summary>
''' An extended version of Textbox that allows replacing A-Z characters with a different
''' set of unicode characters.
''' </summary>
''' <remarks></remarks>
<ToolboxBitmap(GetType(TextBox))> _
Public Class TemplateTextbox
    Inherits TextBox

    Private Const WM_PASTE As Integer = &H302 '770 '0x302 Hexadecimal
    Private Const WM_CHAR As Integer = &H102
    Public Event OnPaste(ByRef e As PastedEventArgs)

    Private Const _defaultFormatString As String = "abcdefghijklmnopqrstuvwxyz"

    Private WithEvents _ParrallelTextBox As New TextBox
    Private _formattingEnabled As Boolean = True
    Private _formatString As String
    Private _SettoDefaultOnError As Boolean = True
    Private _CopyToClipboard As Boolean = False

#Region "Properties"
#Region "FormattingEnabled"
    <Description("Input will not be formatted/styled if this property is set to false. It must be true " & _
                " for the control to style your input.")> _
    Public Property FormattingEnabled() As Boolean
        Get
            Return _formattingEnabled
        End Get
        Set(ByVal value As Boolean)
            _formattingEnabled = value
        End Set
    End Property
#End Region

#Region "CopyToClipboard"
    <Description("Determines if the formatted text is copied into the clipboard as the user types.")> _
    Public Property CopyToClipboard() As Boolean
        Get
            Return _CopyToClipboard
        End Get
        Set(ByVal value As Boolean)
            _CopyToClipboard = value
        End Set
    End Property
#End Region

#Region "SetToDefaultTemplateOnError"
    <Description("Defines the behaviour of control when an error occurs while setting a new tempelate. If set to true" & _
            " no exception will be generated and the TemplateString will be reset to normal small case alphabets. If set to false " & _
            " an exception will be thrown.")> _
    Public Property SetToDefaultTemplateOnError() As Boolean
        Get
            Return _SettoDefaultOnError
        End Get
        Set(ByVal value As Boolean)
            _SettoDefaultOnError = value
        End Set
    End Property
#End Region

#Region "TemplateString"
    <Description("Template string that will be used to style your input.")> _
    Public Property TempelateString() As String
        Get
            Return _formatString
        End Get
        Set(ByVal value As String)
            'Check if it's valid format string
            If value = String.Empty OrElse value.Length <> 26 Then
                _formatString = _defaultFormatString
            Else
                _formatString = value
            End If
        End Set
    End Property
#End Region
#End Region

#Region "Overriden Event"
#Region "WndProc"
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_PASTE   'Notify Upon Paste
                Dim Result As New PastedEventArgs
                RaiseEvent OnPaste(Result)

                'If the event is not handled, pass it on to base class.
                If Result.Handled = False Then GoTo Process_Message
            Case Else
                GoTo Process_Message
        End Select

Exit_Sub:
        Exit Sub


Process_Message:
        MyBase.WndProc(m)
    End Sub
#End Region


#End Region

#Region "Normal Events"
#Region "KeyPress"
    Private Sub InpBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If Me.FormattingEnabled = False Then Exit Sub
        With CType(sender, TemplateTextbox)
            If e.KeyChar = Convert.ToChar(1) Then
                .SelectAll()
                e.Handled = True
                Exit Sub
            End If

            'Replicate the contents to a parrallel textbox so that we can retreive the
            'original text...in future.
            If Not e.Handled Then Call Me._ParrallelTextBox_KeyPress(Me._ParrallelTextBox, e)
            If Utils.IsAlphabet(e.KeyChar) Then
                Try

                    'Write the original text 
                    .Tag = Me._ParrallelTextBox.Text
                    e.KeyChar = Utils.GetNewChar(e.KeyChar, Me.TempelateString)
                Catch ex As Exception

                End Try
            End If
        End With
    End Sub
#End Region

#Region "TextChanged"
    Private Sub InpBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
        If _CopyToClipboard Then Utils.WriteToClipBoard(Me.Text)
    End Sub
#End Region
#End Region
    Private Sub _ParrallelTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles _ParrallelTextBox.KeyPress

    End Sub
End Class